/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import com.lmx.common.constants.ICommonConstants;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.MessageService;
import com.lmx.persistence.service.PayRecordService;
import com.lmx.repoDomain.tmodel.Call;
import com.lmx.repoDomain.tmodel.UserTask;
import com.lmx.repoDomain.tmodel.Workorder;

import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@JsonApiResourceRepository(Workorder.class)
@Component
public class WorkorderRepository extends BaseRepository<Workorder>{
    private Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    private PayRecordService payRecordService;
    
    @Autowired
    private MessageService messageService;

    @PostConstruct
    public void init() {
        logger.debug("init Workorder");
        dslJpaService.setEntityClass(Workorder.class);
    }

    @Override
    @JsonApiSave
    public Workorder save(Workorder workorder) {
        if (workorder.getId() == null) {
           dslJpaService.create(workorder);
        } else {
            String action = ActionContextHolder.getContext().getAction();
            if(StringUtils.isNotBlank(action) && "succOrder".equals(action)){
//                logger.debug("添加成单时间");
//                workorder.setCompleteTime(CommonUtil.getCurrTimes());
            	logger.debug("待确认");
            	
                workorder.setTbcTime(CommonUtil.getCurrTimes());
            	messageService.workorderRemind(workorder,ICommonConstants.THAWTYPE.NOT.ordinal());
            }
            if(StringUtils.isNotBlank(action) && "confirmOrder".equals(action)){
                logger.debug("确认成单");
                payRecordService.confirmOrder(workorder);
                workorder.setCompleteTime(CommonUtil.getCurrTimes());
                messageService.workorderRemind(workorder,ICommonConstants.THAWTYPE.NOT.ordinal());
            }
            if(StringUtils.isNotBlank(action) && "freezeOrder".equals(action)){
                logger.debug("已冻结");
                messageService.workorderRemind(workorder,ICommonConstants.THAWTYPE.NOT.ordinal());
            }
            if(StringUtils.isNotBlank(action) && "thawOrder".equals(action)){
                logger.debug("解冻");
                messageService.workorderRemind(workorder,ICommonConstants.THAWTYPE.COM.ordinal());
            }
            dslJpaService.setEntityClass(Workorder.class);
            dslJpaService.update(workorder);
        }
        return workorder;
    }

}
